const User = require('../model/user.model')


class UserService{
    async createUser(user_name,password){
        //todo: 写入数据库
        // return '写入数据库成功'
        // 插入数据
        // User.create({
        // 表的字段
        // user_name: user_name,
        // password: password 
        // })
        const res = await User.create({user_name,password})
        // console.log(res);


        return res
    }

    async getUserInfo(id,user_name,password,is_admin){
        const whereOpt={}

        id && Object.assign(whereOpt,{id})
        user_name && Object.assign(whereOpt,{user_name})
        password && Object.assign(whereOpt,{password})
        is_admin && Object.assign(whereOpt,{is_admin})

        const res = await User.findOne({
            attributes: ['id','user_name','password','is_admin'],
            where: whereOpt.id
        })
        
        return res ? res.dataValues : null
    }

    async updateById({id,user_name,password,is_admin,repassword}){

        const newUser = {}
        const whereOpt = {id}
        id && Object.assign(newUser,{id})
        user_name && Object.assign(newUser,{user_name})
        password && Object.assign(newUser,{password})

        const res = await User.update(newUser,{where: whereOpt})

        return res[0]>0 ? true : false
    }
    
}

module.exports = new UserService()